<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <!-- 
        什么是函数防抖：事件n秒后触发，不管在这个时间段中执行多少次事件，该事件只针对最后一次事件
        触发
     -->
    <button id="btn">函数防抖</button>
    <script>
        var btn =document.getElementById("btn")
        // btn.onclick =function(){
        //     setTimeout(()=>{
        //         console.log("loading");
        //     },500)
        // }
        var timer;
        btn.onclick =function(){
            if(timer){
                clearTimeout(timer);
            }
            timer =setTimeout(()=>{
                console.log("loaginf");
                timer =null
            },500)
        }
    </script>
</body>
</html>